﻿#region licence
//  Copyright 2008 Michael Cromwell

//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at

//       http://www.apache.org/licenses/LICENSE-2.0

//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net.Repository.Hierarchy;
using log4net.Repository;
using log4net.Core;
using log4net.Util;

namespace log4net.altconf
{
    /// <summary>
    /// Repository specific for creating loggers based on configuration from
    /// DSL file
    /// </summary>
    public class DslRepository : Hierarchy, IDslRepositoryConfigurator
    {
        private static readonly Type declaringType = typeof(DslRepository);
        private const string rootLoggerName = "root";
        
        public DslRepository()
        {
        }

        /// <summary>
        /// Tells this instance to configure itself using the config provided
        /// </summary>
        /// <param name="config">configuration to use</param>
        public void Configure(Log4NetDslConfiguration config)
        {
            // perform reset so we start from scratch
            ResetConfiguration(); 

            LogLog.InternalDebugging = config.debug;
            Threshold = config.threshold;
            LogLog.Debug("Hierarchy Threshold [" + config.threshold + "]");
            
            foreach (var log in config.Logs)
            {
                Logger logger = null;
                if (log.Name.Equals(rootLoggerName, StringComparison.InvariantCultureIgnoreCase))
                    logger = Root;
                else
                    logger = GetLogger(log.Name) as Logger;

                LogLog.Debug("Retrieving an instance of log4net.Repository.Logger for logger [" + log.Name + "].");
                lock (logger)
                {
                    logger.RemoveAllAppenders();
                    foreach (var appender in log.Appenders)
                    {
                        LogLog.Debug("Adding appender named [" + appender.Name + "] to logger [" + log.Name + "].");
                        IOptionHandler optionHandler = appender as IOptionHandler;
                        if (optionHandler != null)
                            optionHandler.ActivateOptions();
                        
                        logger.AddAppender(appender);
                    }
                    logger.Level = log.Level;
                    logger.Additivity = log.Additivity;
                    LogLog.Debug("Logger [" + log.Name + "] level set to [name=\"" + log.Level.Name + "\",value=" + log.Level.Value + "].");
                }
            }
            Configured = true;
        }
        
        
    }

}
